﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace MailboxManagerFree
{
    public partial class UI : Form
    {
        //For use in adding items to autocomplete when user is entering the object
        AutoCompleteStringCollection source = new AutoCompleteStringCollection();
        DataGridViewTextBoxColumn recipColumn = new DataGridViewTextBoxColumn();
        DataGridViewCheckBoxColumn codColumn = new DataGridViewCheckBoxColumn();
        DataGridViewTextBoxColumn objColumn = new DataGridViewTextBoxColumn();
        DataGridViewTextBoxColumn priceColumn = new DataGridViewTextBoxColumn();
        DataGridViewCheckBoxColumn stackColumn = new DataGridViewCheckBoxColumn();

        public UI()
        {
            InitializeComponent();

            var sortedList =
                    (from d in MailboxManager.items
                     orderby !d.CoD, d.Recipient, d.Object
                     select d).ToList();

            MailboxManager.items = new BindingList<Item>(sortedList);

            mailingListGrid.AutoGenerateColumns = false;

            source.AddRange(MailboxManager.tradeItems.ChantMats.Select(i => i.Key).ToArray());
            source.AddRange(MailboxManager.tradeItems.CommonChants.Select(i => i.Key).ToArray());
            source.AddRange(MailboxManager.tradeItems.CommonGems.Select(i => i.Key).ToArray());
            source.AddRange(MailboxManager.tradeItems.Elixers.Select(i => i.Key).ToArray());
            source.AddRange(MailboxManager.tradeItems.Flasks.Select(i => i.Key).ToArray());
            source.AddRange(MailboxManager.tradeItems.Herbs.Select(i => i.Key).ToArray());
            source.AddRange(MailboxManager.tradeItems.InksPigments.Select(i => i.Key).ToArray());
            source.AddRange(MailboxManager.tradeItems.LegArmors.Select(i => i.Key).ToArray());
            source.AddRange(MailboxManager.tradeItems.Ores.Select(i => i.Key).ToArray());
            source.AddRange(MailboxManager.tradeItems.Potions.Select(i => i.Key).ToArray());
            source.AddRange(MailboxManager.tradeItems.RareChants.Select(i => i.Key).ToArray());
            source.AddRange(MailboxManager.tradeItems.RareGems.Select(i => i.Key).ToArray());
            source.AddRange(MailboxManager.tradeItems.RingsNecks.Select(i => i.Key).ToArray());
            source.AddRange(MailboxManager.tradeItems.Skins.Select(i => i.Key).ToArray());
            
            
            recipColumn.DataPropertyName = "Recipient";
            recipColumn.HeaderText = "Recipient Name";

            codColumn.DataPropertyName = "CoD";
            codColumn.HeaderText = "CoD";

            objColumn.DataPropertyName = "Object";
            objColumn.HeaderText = "Game Object Name";
            
            priceColumn.DataPropertyName = "UIPrice";
            priceColumn.HeaderText = "Price per Stack";

            stackColumn.DataPropertyName = "FullStacks";
            stackColumn.HeaderText = "Full Stacks Only";

            mailingListGrid.Columns.Add(recipColumn);
            mailingListGrid.Columns.Add(codColumn);
            mailingListGrid.Columns.Add(objColumn);
            mailingListGrid.Columns.Add(priceColumn);
            mailingListGrid.Columns.Add(stackColumn);
            
            mailingListGrid.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;

            mailingListGrid.DataSource = MailboxManager.items;

            /*mailingListGrid.Columns["Recipient"].DataPropertyName = "Recipient";
            mailingListGrid.Columns["CoD"].DataPropertyName = "CoD";
            mailingListGrid.Columns["Object"].DataPropertyName = "Object";
            mailingListGrid.Columns["Price"].DataPropertyName = "Price";
            mailingListGrid.Columns["FullStacks"].DataPropertyName = "FullStacks";*/
            
        }

        private void buttonExport_Click(object sender, EventArgs e)
        {
            MailboxManager.ExportSettings();
        }

        private void buttonImport_Click(object sender, EventArgs e)
        {
            MailboxManager.ImportSettings();
            mailingListGrid.DataSource = null;
            mailingListGrid.DataSource = MailboxManager.items;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MailboxManager.LoadSettings();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            MailboxManager.SaveSettings();
        }

        private void mailingListGrid_CellParsing(object sender, DataGridViewCellParsingEventArgs e)
        {
            if (mailingListGrid.Columns[e.ColumnIndex].DataPropertyName == "Price")
            {
                int gold = 0;
                int silver = 0;
                int copper = 0;
                if (e.Value != null)
                {
                    try
                    {
                        var moneyString = e.Value.ToString().ToLowerInvariant();
                        var match = Regex.Match(moneyString, "([0-9]+(g|s|c))");
                        if (match.Success)
                        {
                            var goldRegex = Regex.Match(moneyString, "([0-9]+g)");
                            if (goldRegex.Success)
                            {
                                gold = int.Parse(goldRegex.Value.Replace("g", ""));
                            }
                            var silverRegex = Regex.Match(moneyString, "([0-9]+s)");
                            if (silverRegex.Success)
                            {
                                silver = int.Parse(silverRegex.Value.Replace("s", ""));
                            }
                            var copperRegex = Regex.Match(moneyString, "([0-9]+c)");
                            if (copperRegex.Success)
                            {
                                copper = int.Parse(copperRegex.Value.Replace("c", ""));
                            }
                        }
                        else
                        {
                            int.TryParse(e.Value.ToString(), out copper);
                        }
                        e.Value = copper + (silver * 100) + (gold * 10000);
                        e.ParsingApplied = true;
                    }
                    catch (Exception)
                    {
                        e.ParsingApplied = false;
                        throw;
                    }
                }
            }
        }

        private void mailingListGrid_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (mailingListGrid.Columns[e.ColumnIndex].DataPropertyName == "Price")
            {
                if (e.Value != null)
                {
                    try
                    {
                        e.Value = Money.ToString((int)e.Value);
                    }
                    catch (Exception)
                    {
                        e.FormattingApplied = false;
                        MailboxManager.LogS("Exception Thrown - Line 143 MailboxManager.cs");
                    }
                }
            }
        }

        private void buttonPause_Click(object sender, EventArgs e)
        {
            MailboxManager.myset.PausePlugin = !MailboxManager.myset.PausePlugin;
        }

        private void mailingListGrid_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            TextBox editBox = e.Control as TextBox;
            if (editBox != null)
            {
                if (mailingListGrid.SelectedCells[0].ColumnIndex == 2)
                {
                    editBox.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                    editBox.AutoCompleteCustomSource = source;
                    editBox.AutoCompleteSource = AutoCompleteSource.CustomSource;
                }
            }
        }

    }
}
